package com.xw.tools.view;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ZipUtil;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import java.io.File;

public class ZipWin extends AbstractWin {

    public void start() {
        Stage primaryStage = new Stage();
        primaryStage.setTitle("文件夹压缩");
        Scene scene = new Scene(new VBox(), 800, 495);
        initView(scene, primaryStage);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void initView(Scene scene, Stage primaryStage) {
        TextField textField = new TextField();
        Button fileBtn = new Button("请选择文件夹");
        fileBtn.setOnAction((ActionEvent t) -> {
            DirectoryChooser fileChooser = new DirectoryChooser();
            File file = fileChooser.showDialog(primaryStage);
            if (file != null && file.exists()) {
                textField.setText(file.getAbsolutePath());
            }
        });

        ChoiceBox cb = new ChoiceBox(FXCollections.observableArrayList(
                "包含当前文件夹", "不包含当前文件夹")
        );
        cb.setValue("包含当前文件夹");

        TextField textField2 = new TextField();
        Button fileBtn2 = new Button("压缩后文件");
        fileBtn2.setOnAction((ActionEvent t) -> {
            FileChooser fileChooser = new FileChooser();
            File file = fileChooser.showSaveDialog(primaryStage);
            if (file != null) {
                textField2.setText(file.getAbsolutePath());
            }
        });

        Button button = new Button("压缩");
        button.setOnAction((ActionEvent t) -> {
            String filePath = textField.getText();
            String zip = textField2.getText();
            if (StrUtil.isBlank(filePath) || StrUtil.isBlank(zip)) {
                return;
            }
            File zipPathFile = new File(filePath);
            if (!zipPathFile.exists()) {
                Alert alert = new Alert(Alert.AlertType.ERROR);
                alert.setHeaderText("文件夹不存在!");
                alert.showAndWait();
                return;
            }

//            FileChooser fileChooser = new FileChooser();
//            FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("zip files (*.zip|*.*)",
//                    "*.zip|*.*");
//            fileChooser.getExtensionFilters().add(extFilter);
//            File file = fileChooser.showSaveDialog(primaryStage);
//            if (file == null) {
//                return;
//            }

            boolean b = true;
            if ("不包含当前文件夹".equals(cb.getValue())) {
                b = false;
            }

            // 压缩
            ZipUtil.zip(new File(zip), b, zipPathFile);

//            Alert alert = new Alert(Alert.AlertType.INFORMATION);
//            alert.setHeaderText("压缩成功!");
//            alert.show();
            return;

        });
        VBox box = (VBox) scene.getRoot();
        HBox hBox = new HBox(10);
        hBox.getChildren().add(textField);
        hBox.getChildren().add(fileBtn);
        HBox.setHgrow(textField, Priority.ALWAYS);
        HBox.setHgrow(fileBtn, Priority.ALWAYS);

        HBox hBox2 = new HBox(10);
        hBox2.getChildren().add(textField2);
        hBox2.getChildren().add(fileBtn2);
        HBox.setHgrow(textField2, Priority.ALWAYS);
        HBox.setHgrow(fileBtn2, Priority.ALWAYS);

        box.getChildren().add(hBox);
        box.getChildren().add(cb);
        box.getChildren().add(hBox2);
        box.getChildren().add(button);
    }

    @Override
    public void show() {
        start();
    }
}
